/**
 * Copyright (c) 2007,中企动力华南研发中心<br>
 * All rights reserved.<br>
 */
package com.ce.pms.report.service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.ce.pms.charge.dao.INoteDao;
import com.ce.pms.charge.service.INoteService;
import com.ce.pms.charge.vo.NoteVO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.report.vo.PrintChargeNoteBean;
import com.ce.pms.report.vo.PrintChargeNoteQueryVO;
import com.ce.pms.report.vo.PrintChargeNoteSubVO;
import com.ce.pms.report.vo.PrintChargeNoteVO;
import com.ce.report.entity.CEReportDataSet;
import com.ce.report.exception.CEReportException;
import com.ce.report.service.ICEReportDataSetService;
import com.ce.report.vo.ReportsQueryBaseVo;
import com.ce.util.DateUtil;
import com.ce.util.RmbUtil;


/**
 * 功能描述：打印通知单Service<br>
 * @author 易贤建 创建日期：2008-9-26<br>
 */
public class PrintChargeNoteService implements ICEReportDataSetService{

	private INoteDao noteDao;
	private INoteService noteService;

	public CEReportDataSet getReportDataSet(ReportsQueryBaseVo queryVO) throws CEReportException {
		PrintChargeNoteQueryVO pringChargeNoteQueryVO = (PrintChargeNoteQueryVO)queryVO;

		List<PrintChargeNoteBean> beanList = noteService.queryPrintChargeNote(converQueryBaseVOToNoteVO(pringChargeNoteQueryVO));
		List<PrintChargeNoteVO> printChargeNoteList = converBeanToVO(beanList);
		//处理没一个报表,确保有13条明细记录
		for(Iterator<PrintChargeNoteVO> iterator = printChargeNoteList.iterator();iterator.hasNext();)
			processSubList(iterator.next().getSubList());
		return new CEReportDataSet(printChargeNoteList);
	}

	/**
	 * 功能描述: 将查询VO转换成noteVO<br>
	 * @param pringChargeNoteQueryVO
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-10-9  <br>
	 */
	private NoteVO converQueryBaseVOToNoteVO(PrintChargeNoteQueryVO pringChargeNoteQueryVO){
		NoteVO noteVO = new NoteVO();
		noteVO.setNoteId(pringChargeNoteQueryVO.getNoteId());
		noteVO.setBuildingId(pringChargeNoteQueryVO.getBuildingId());
		noteVO.setHouseName(pringChargeNoteQueryVO.getHouseName());
		noteVO.setValidity(pringChargeNoteQueryVO.getValidity());
		noteVO.setPeriodId(pringChargeNoteQueryVO.getPeriodId());
		noteVO.setRequisitionNum(pringChargeNoteQueryVO.getRequisitionNum());//单号
		noteVO.setIsVirtualHouse(pringChargeNoteQueryVO.getIsVirtualHouse());//打印方式
		return noteVO;
	}

	/**
	 * 功能描述: 将查询的结果bean转换为VO<br>
	 * @param noteDomain
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-10-8  <br>
	 */
	private List<PrintChargeNoteVO> converBeanToVO(List<PrintChargeNoteBean> printChargeNoteBean){
		List<PrintChargeNoteVO> printChargeNotelist = new ArrayList<PrintChargeNoteVO>();
		if(printChargeNoteBean == null) return printChargeNotelist;

		Iterator<PrintChargeNoteBean> iterator = printChargeNoteBean.iterator();
		PrintChargeNoteVO printChargeNoteVO = null;
		while(iterator.hasNext()){
			PrintChargeNoteBean noteBean  = iterator.next();
			//如果printChargeNoteVO 为空,则说明是第一个记录。如果通知单号不等,则说明是下一个待打印的通知单,这样就需要处理母报表
			if(printChargeNoteVO == null || !(printChargeNoteVO.getNoteId().longValue() == noteBean.getNoteId().longValue())){
				printChargeNoteVO = converBeanToPringChargeNoteVO(noteBean); //处理母报表字段
				printChargeNotelist.add(printChargeNoteVO);
			}
			//添加为子报表数据
			printChargeNoteVO.getSubList().add(converBeanToVO(noteBean));
		}
		return printChargeNotelist;
	}

	/**
	 * 功能描述: 将查询的结果保存母报表需要的数据<br>
	 * @param noteBean
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-10-9  <br>
	 */
	private PrintChargeNoteVO converBeanToPringChargeNoteVO(PrintChargeNoteBean noteBean){
		if(noteBean == null) return null;

		PrintChargeNoteVO noteVO = new PrintChargeNoteVO();

		noteVO.setNoteId(noteBean.getNoteId()); //通知单ID
		noteVO.setVoucherDate(processDateStr(noteBean.getVoucherDate()));
		noteVO.setHouseNumber(noteBean.getHouseNumber());
		noteVO.setParticipantName(noteBean.getParticipantName());
		noteVO.setBuildSize(noteBean.getBuildSize());
		noteVO.setReceivable(noteBean.getReceivableTotal()); //本月应缴
		noteVO.setReceivableThisTotal(noteBean.getReceivableThisTotal());//本月金额总计
		noteVO.setReceivableOldTotal(noteBean.getReceivableOldTotal());//往月欠费合计
		noteVO.setReceivableTotal(noteBean.getReceivableTotal());//本月应收合计
		//处理大写问题
		noteVO.setCapitalization(RmbUtil.getRMB(noteBean.getReceivableTotal().doubleValue()));
		return noteVO;
	}
	/**
	 * 功能描述:将查询的结果转换为子报表对应的VO<br>
	 * @param noteBean
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-10-9  <br>
	 */
	private PrintChargeNoteSubVO converBeanToVO(PrintChargeNoteBean noteBean){
		if(noteBean == null) return null;
		PrintChargeNoteSubVO subVO = new PrintChargeNoteSubVO();

		subVO.setChargeItem(noteBean.getChargeItem());
		subVO.setThisAmt(noteBean.getThisAmt());
		subVO.setOldAmt(noteBean.getOldAmt());
		subVO.setThisTotalAmt(noteBean.getThisTotalAmt());
		subVO.setThisReading(noteBean.getThisReading());
		subVO.setLastReading(noteBean.getLastReading());
		subVO.setDosage(noteBean.getDosage());
		subVO.setPrice(noteBean.getPrice());
		subVO.setPrice1(noteBean.getPrice1());
		subVO.setPrice2(noteBean.getPrice2());
		return subVO;
	}
	/**
	 * 功能描述:处理子报表,确保子报表数据只有13条数据<br>
	 * @param subList
	 * Create author:yixianjian <br>
	 * Create on:2008-10-9  <br>
	 */
	private void processSubList(List<PrintChargeNoteSubVO> subList){
		if(subList == null) return;
		int count = subList.size();
		if(count >= 13){
			for(int i = count - 13;i > 0; i--){
				subList.remove(i + 12);
			}
		}else if(count < 13){
			for(int i = 13 - count; i > 0; i--){
				subList.add(new PrintChargeNoteSubVO());
			}
		}
	}
	/**
	 * 功能描述:将字符串200809转换为2008年09月<br>
	 * @param dateStr
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-10-9  <br>
	 */
	private String processDateStr(String dateStr){
		try {
			return DateUtil.convertDateToStr(DateUtil.parseStrToDate(dateStr,"yyyyMM"), "yyyy年MM月");
		} catch (ParseException e) {
			throw new BusinessException("Report exception:cast data failuer");
		}
	}
	public void setNoteDao(INoteDao noteDao) {
		this.noteDao = noteDao;
	}

	public void setNoteService(INoteService noteService) {
		this.noteService = noteService;
	}


}
